require( "gip")
require( "util")

gip.init()

local basePath
local genPath
local directionNum
local baseX
local baseY
local failedCount
local successCount
local descFailedCount

local srcImageSuffix = ".png"

local function imageFileCallBack( filename, isFile)
    filename = string.lower(filename)
    local dotPos = string.find( filename, srcImageSuffix, #filename - 4)
    if dotPos == nil then
        return
    end
    
    local image = gip.load( filename);
    if image == nil then
        print( "\n")
        print( filename .. "load failed!")
        failedCount = failedCount + 1
        return
    end
    
    local x, y, w, h = image.validArea()
    io.write( ".")
    local newImage = image.cut( x, y, w, h)
    image = nil
    
    local relPath = string.sub( filename, #basePath + 1, #filename - 4)
    if directionNum == 1 then
        if string.find( relPath, "0") == 1 then
            relPath = "4" .. string.sub( relPath, 1, #relPath - 1)
        end
    elseif directionNum == 3 then
        if string.find( relPath, "0") == 1 then
            relPath = "3" .. string.sub( relPath, 1, #relPath - 1)
        elseif string.find( relPath, "1") == 1 then
            relPath = "4" .. string.sub( relPath, 1, #relPath - 1)
        elseif string.find( relPath, "2") == 1 then
            relPath = "5" .. string.sub( relPath, 1, #relPath - 1)
        end
    elseif directionNum == 4 then
        if string.find( relPath, "0") == 1 then
            relPath = "1" .. string.sub( relPath, 1, #relPath - 1)
        elseif string.find( relPath, "1") == 1 then
            relPath = "3" .. string.sub( relPath, 1, #relPath - 1)
        elseif string.find( relPath, "2") == 1 then
            relPath = "5" .. string.sub( relPath, 1, #relPath - 1)
        elseif string.find( relPath, "3") == 1 then
            relPath = "7" .. string.sub( relPath, 1, #relPath - 1)
        end
    end
    relPath = relPath .. ".jai"
    local newPath = genPath .. relPath
    local ret = newImage.save( newPath)
    if not ret then
        print( "\n")
        print( filename .. "failed!")
        failedCount = failedCount + 1
    else
        successCount = successCount + 1
        local descFileName, imageFileName = splitPath( newPath)
        descFileName = descFileName .. "desc.txt"
        local descFile = io.open( descFileName, "a")
        if descFile ~= nil then
            local s = string.format( "%s %d %d %d %d\r\n", imageFileName, 
                x - baseX, y - baseY, w, h)
            descFile:write( s)
            descFile:close()
        else
            print( "write desc failed: " .. newPath)
            descFailedCount = descFialed + 1
        end
    end 
    --getmetatable( newImage).__gc()
end

function spriteCallback( filename, isFile)
    if isFile then
        return
    end
    gip.transDir( filename, imageFileCallBack)
    
    --local partList = {}
    local partList = ""
    local first = true
    function partCallback( filename, isFile)
        if not isFile then
            local _, partDir = splitPath( filename)
            --table.insert( partList, partDir)
            if first then
                partList = partDir
                first = false
            else
                partList = partList .. " " .. partDir
            end
        end
    end
    gip.transDir( filename, partCallback, false)
    
    local actList = ""
    first = true
    function actionCallback( filename, isFile)
        if not isFile then
            local _, actDir = splitPath( filename)
            --table.insert( actList, actDir)
            if first then
                actList = actDir .. "%8"
                first = false
            else
                actList = actList .. " " .. actDir .. "%8"
            end
        end
    end
    local bodyPath = filename .. "/body"
    gip.transDir( bodyPath, actionCallback, false);
    
    local _, lastDir = splitPath( filename)
    local descFileName = string.format( "%s/%s/desc.txt", genPath, lastDir)
    local descFile = io.open( descFileName, "w")
    if descFile ~= nil then
        local s = string.format( "direction %d\r\n", directionNum)
        descFile:write( s)
        if #partList > 0 then
            descFile:write( "parts ")
            --for _, k in ipairs(parList) do
                --descFile:write( k)
                --descFile:write( " ")
            --end
            descFile:write( partList)
            descFile:write( "\r\n")
            descFile:write( "actions ")
            descFile:write( actList)
        end
        descFile:close()
    end
end

function genSprite( srcPath, dstPath, drtNum, bx, by)
    basePath = srcPath
    genPath = dstPath
    directionNum = dirNum or 8
    baseX = bx or 256
    baseY = by or 282
    failedCount = 0
    successCount = 0
    descFailedCount = 0
    
    gip.setJpgQuality( 75)
    gip.transDir( basePath, spriteCallback, false)
    
    if failedCount == 0 and descFailedCount == 0 then
        print( "It's all OK!")
    else
        local s = string.format( "success: %d, failed: %d, desc fialed: %d",
            successCount, failedCount, descFailedCount)
        print( s)
    end
end

--for monster
srcImageSuffix = ".png"
--genSprite( "/export/arts/monster_png", "/export/arts/monster_pak/", 8)
--srcImageSuffix = ".tga"
--genSprite( "/export/arts/character_tga", "/export/arts/character_pak", 8)
genSprite( "/home/wzp/dtools/gip/tools", "/home/wzp/dtools/gip/", 8)
gip.deInit()

